//
//  ViewController.m
//  HTTestPro
//
//  Created by JunWang on 16/3/3.
//  Copyright © 2016年 JunWang. All rights reserved.
//

#import "MainViewController.h"
#import "WJJCalendarView.h"


@interface MainViewController ()
@property (nonatomic, strong)  WJJCalendarView *calendarView;
@end

@implementation MainViewController

-(WJJCalendarView *)calendarView
{
    if (!_calendarView) {
        _calendarView = [[WJJCalendarView alloc]initWithFrame:[UIScreen mainScreen].bounds];
        [self.view addSubview:_calendarView];
    }
    return _calendarView;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.calendarView.delegate = self;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}


#pragma mark - DSLCalendarViewDelegate methods

- (void)calendarView:(WJJCalendarView *)calendarView didSelectRange:(WJJCalendarRange *)range {
    if (range != nil) {
        NSLog( @"Selected %d/%d - %d/%d", range.startDay.day, range.startDay.month, range.endDay.day, range.endDay.month);
    }
    else {
        NSLog( @"No selection" );
    }
}

- (WJJCalendarRange*)calendarView:(WJJCalendarView *)calendarView didDragToDay:(NSDateComponents *)day selectingRange:(WJJCalendarRange *)range {
    if (NO) { // Only select a single day
        return [[WJJCalendarRange alloc] initWithStartDay:day endDay:day];
    }
    else if (NO) { // Don't allow selections before today
        NSDateComponents *today = [[NSDate date] dslCalendarView_dayWithCalendar:calendarView.visibleMonth.calendar];
        
        NSDateComponents *startDate = range.startDay;
        NSDateComponents *endDate = range.endDay;
        
        if ([self day:startDate isBeforeDay:today] && [self day:endDate isBeforeDay:today]) {
            return nil;
        }
        else {
            if ([self day:startDate isBeforeDay:today]) {
                startDate = [today copy];
            }
            if ([self day:endDate isBeforeDay:today]) {
                endDate = [today copy];
            }
            
            return [[WJJCalendarRange alloc] initWithStartDay:startDate endDay:endDate];
        }
    }
    
    return range;
}

- (void)calendarView:(WJJCalendarView *)calendarView willChangeToVisibleMonth:(NSDateComponents *)month duration:(NSTimeInterval)duration {
    NSLog(@"Will show %@ in %.3f seconds", month, duration);
}

- (void)calendarView:(WJJCalendarView *)calendarView didChangeToVisibleMonth:(NSDateComponents *)month {
    NSLog(@"Now showing %@", month);
}

- (BOOL)day:(NSDateComponents*)day1 isBeforeDay:(NSDateComponents*)day2 {
    return ([day1.date compare:day2.date] == NSOrderedAscending);
}

-(BOOL)prefersStatusBarHidden
{
    return true;
}
@end

